const fs = require('fs')
const { filterFiles, readPrefab } = require("./utils");

const SCALE = 0.5;

// const testDir = 'C:/Users/chengguoqiang/Desktop/TestForCC/assets/'
const testDir = 'D:/client/client_wbqj_fix_sync_final/assets/resources/gl_zh/'

let files = filterFiles(testDir, '.prefab', true);
// let files = filterFiles(testDir, '.fire', true);



//DONE: Node Label RichText Sprite Widget Layout UISuperLayout   (Prefab Scene)
//TODO: plist spine



let targets = [];
files.forEach(f => {
    // console.log('scale pf', f)
    let pf = readPrefab(f);
    // console.log('scale pf----------1', pf)
    
    pf.forEach(p => {
        // console.log('scale pf+++++++++++++', p)
        if (p.__type__ == 'cc.Node') {
            let _comps = p._components;
            let _renderComp;
            let _widget;
            let _layout;
            let _superLayout;
            _comps.forEach(c => {
                let _cmp = pf[c.__id__];
                let _type = _cmp.__type__;
                if (_type == 'cc.Sprite' || _type == 'cc.Label' || _type == 'cc.RichText') {
                    _renderComp = _cmp;
                }
                if (_type == 'cc.Widget') {
                    _widget = _cmp;
                }
                if (_type == 'cc.Layout') {
                    _layout = _cmp;
                }
                if (_type == "430e1YPKIJAwqNszj7fRubB") {
                    _superLayout = _cmp;
                }
            });
            //widget优先级最高  需查看widget是否影响size


            let skip = {
                w: false,
                h: false
            }
            if (_widget) {
                skip = scaleWidget(_widget);
            }
            if (_renderComp) {
                switch (_renderComp.__type__) {
                    case 'cc.Sprite':
                        scaleSprite(p, _renderComp);
                        break;
                    case 'cc.Label':
                        scaleLabel(_renderComp);
                    case 'cc.RichText':
                        scaleRichText(_renderComp);
                        break;

                }
            } else {
                scaleNode(p, skip)
            }

            if (_layout) {
                scaleLayout(_layout);
            }
            if (_superLayout) {
                scaleSuperLayout(_superLayout)
            }

            relocateNode(p)
        }
    })

    fs.writeFileSync(f, JSON.stringify(pf, null, 2));
})


function scaleSuperLayout(layout) {
    /**
     *  paddingBottom: 0
        paddingLeft: 0
        paddingRight:0
        paddingTop: 10

        spacing
            x: 0
            y: 16
     */
    console.log('scale superLayout')
    layout.paddingBottom *= SCALE;
    layout.paddingLeft *= SCALE;
    layout.paddingRight *= SCALE;
    layout.paddingTop *= SCALE;

    layout.spacing.x *= SCALE;
    layout.spacing.y *= SCALE;

}

function scaleLayout(layout) {
    //spacing 
    layout._N$paddingLeft *= SCALE;
    layout._N$paddingRight *= SCALE;
    layout._N$paddingTop *= SCALE;
    layout._N$paddingBottom *= SCALE;

    layout._N$spacingX *= SCALE;
    layout._N$spacingY *= SCALE;

    layout._N$cellSize.width *= SCALE;
    layout._N$cellSize.height *= SCALE;
}

function scaleWidget(widget) {

    let flag = widget._alignFlags;
    console.log('scale widget', flag)
    let horizonCenter = getLocValue(flag, 4);
    let verticleCenter = getLocValue(flag, 1);
    let top = getLocValue(flag, 0);
    let left = getLocValue(flag, 3);
    let bottom = getLocValue(flag, 2);
    let right = getLocValue(flag, 5);

    if (top) {
        widget._top *= SCALE;
    }
    if (left) {
        widget._left *= SCALE;
    }
    if (bottom) {
        widget._bottom *= SCALE;
    }
    if (right) {
        widget._right *= SCALE;
    }

    if (horizonCenter) {
        widget._horizontalCenter *= SCALE;
    }
    if (verticleCenter) {
        widget._verticalCenter *= SCALE;
    }

    return {
        w: left && right,
        h: top && bottom
    }

}


function scaleLabel(comp) {
    comp._fontSize *= SCALE;
    comp._lineHeight *= SCALE;
    console.log('scale label', comp._fontSize)
}
function scaleRichText(comp) {
    console.log('scale richtext')
    comp._N$lineHeight *= SCALE;
    comp._N$maxWidth *= SCALE;
    comp._N$fontSize *= SCALE;
}
function scaleSprite(node, comp) {
    console.log('sprite ', comp._sizeMode)
    if (comp._sizeMode == 0) {
        scaleNode(node)
        console.log('scale custom sprite')
    } else {
        return {
            w: true,
            h: true
        }
    }

}
function relocateNode(item) {
    item._trs.array[0] *= SCALE;
    item._trs.array[1] *= SCALE;
    console.log('relocate node', item._name);
}
function scaleNode(item) {
    console.log('scale node', item._name, item._contentSize.width, item._contentSize.height)
    item._contentSize.width *= SCALE;
    item._contentSize.height *= SCALE;
    console.log('scale node', item._name, item._contentSize.width, item._contentSize.height)

}


function getLocValue(num, pos) {
    let _b = num.toString(2);
    return _b[_b.length - pos - 1]
}